/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 
 
#ifndef _NUC900_GE_H_
#define _NUC900_GE_H_

#include "wbtypes.h"
//#include "wblib.h" //cmn

//#define printf sysprintf  //for using uart
//#define scanf sysGetChar   //smf
/*
#undef printf
#undef getchar
#ifdef ECOS
#define getchar		diag_getc
#define printf 		diag_printf
#else
#define getchar		sysGetChar
#define printf 		sysprintf
#endif
*/

/*--- compiler options ---*/

/*
** Changes in REV6:
** (1) The Tile X and Tile Y control register is changed.
** (2) The Filter can support sign bit in central pixel
**     coefficient. 
** (3) Filter clamping maximum value is programmable. 
**
*/
#define GE_REV6
#define GE_RESET
#define USE_DUMP_FILE // if you really want to dump the screen
//#define USE_PSEUDO_WRITE // for FPGA software add pseudo write

//
// Known Bugs:
// (1) solid fill has problem in 16-bpp and 32-bpp at 1:1 clock
// (2) meet once in 16-bpp host read/write at 1:2 clock.
// (3) mono pattern ROP (0x00 or 0xff) has problem in 8-bpp and 16-bpp 
//     at 1:2 and 1:4 clock, 32-bpp has problem in 1:4 clock
// 
// NOTE: After the problem above happen, it is no use to 
//       reset the engine to recover the status.
//
//
#define AUTO_DUMP
#define VERIFY_CLIP
#define VERIFY_SOLID
#define VERIFY_MONO_PAT
#define VERIFY_COLOR_PAT
#define VERIFY_BITBLT
#define VERIFY_HOST_WRITE
#define VERIFY_ROP
#define VERIFY_TEXT_BLT
#define VERIFY_TRANSPARENT
#define VERIFY_TILE_BLT
#define VERIFY_ALPHA
#define VERIFY_LINE
#define VERIFY_HOST_READ_WRITE

//#define CHECK_REGISTER
//#define CHECK_TEXT_BLT
//#define CHECK_HOST_TEXT_BLT
//#define CHECK_SOLID_FILL
//#define CHECK_COLOR_PAT_FILL
//#define CHECK_MONO_PAT_FILL
//#define CHECK_SCREEN_TO_SCREEN_BLT
//#define CHECK_SCREEN_TO_SCREEN_ROP_BLT
//#define CHECK_SCREEN_SPRITE_BLT 
//#define CHECK_SCREEN_SPRITE_BLTX
//#define CHECK_HOST_SPRITE_BLT
//#define CHECK_ROP256
//#define CHECK_ALPHA
//#define CHECK_ALPHA2
//#define CHECK_KSKD
//#define CHECK_HOST_WRITE_BLT
//#define CHECK_HOST_READ_BLT
//#define CHECK_HOST_READ_WRITE
//#define CHECK_LINE_DRAW
//#define CHECK_STYLED_LINE_DRAW
//#define CHECK_FRAME_DRAW
//#define CHECK_FILTER
//#define CHECK_FILTER_444
//#define CHECK_FILTER_422
//#define CHECK_FILTER_420
//#define CHECK_TILE_BLT
//#define CHECK_CLIP
//#define CHECK_TRANSPARENT
//#define CHECK_WRITE_MASK
//#define CHECK_BLT_OCTANT
//#define CHECK_MEM_COPY
//#define CHECK_JPEG
//#define TEST_STRETCH_BLT  //cmn
//#define TEST_ROTATE //cmn

/* function return value  */
#define	   Successful  0
#define	   Fail        1

#define DEMO_FONT

#define TOPPOLY_LCM
#define SAMSUNG_LCM

// R=100, G=150, B=200
#define COLOR_KEY 0x6496c8

#if 0

#define DUMP_ROOT_8BPP      "E:\\NUC900\\8BPP\\"
#define DUMP_ROOT_16BPP     "E:\\NUC900\\16BPP\\"
#define DUMP_ROOT_32BPP     "E:\\NUC900\\32BPP\\"

//D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern
#define COLOR_PATTERN_FILE  "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\PATC.DAT"
#define MONO_PATTERN_FILE	"D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\PATM.DAT"
#define IMAGE_40_FILE       "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\CHI40.DAT"
#define IMAGE_160_FILE      "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\NUDE160.DAT"
#define IMAGE_320_FILE      "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\SHU320.DAT"
#define IMAGE_KEY_FILE      "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\KEY320.DAT"
#define SCREEN_DUMP_FILE	"D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\SCREEN.DAT"
#define MEM_DUMP_FILE       "D:\\Documents and Settings\\SMFan\\Desktop\\TEST_PROGRAM\\GE\\Pattern\\MEM.DAT"
#endif

#define CMODEL_DUMP_FILE	"C:\\CMODEL.DAT"
#define REG_DUMP_FILE		"C:\\REG.DAT"


///////////////////////////////////////////////////////////////////////////////
// Definition of ROP2
///////////////////////////////////////////////////////////////////////////////
#define    BLACKNESS       0x00  // 0
#define    DSTINVERT       0x55  // Dn
#define    MERGECOPY       0xC0  // PSa
#define    MERGEPAINT      0xBB  // DSno
#define    NOTSRCCOPY      0x33  // Sn
#define    NOTSRCERASE     0x11  // DSon
#define    PATCOPY         0xF0  // P
#define    PATINVERT       0x5A  // DPx
#define    PATPAINT        0xFB  // DPSnoo
#define    SRCAND          0x88  // DSa
#define    SRCCOPY         0xCC  // S
#define    SRCERASE        0x44  // SDna
#define    SRCINVERT       0x66  // DSx
#define    SRCPAINT        0xEE  // DSo
#define    WHITENESS       0xFF  // 1

///////////////////////////////////////////////////////////////////////////////
// Definition of Pen Styles
///////////////////////////////////////////////////////////////////////////////
#define PS_SOLID   		0xffff	   //1111111111111111 (1111111111111111)
#define PS_DASH	   		0xcccc	   //1100110011001100 (1111000011110000)
#define PS_DOT	   		0xaaaa	   //1010101010101010 (1100110011001100)
#define PS_DASHDOT	   	0xe4e4	   //1110010011100100 (1111110000110000)
#define PS_DASHDOTDOT   0xeaea	   //1110101011101010 (1111110011001100)
#define PS_NULL	   		0x0000	   //0000000000000000 (0000000000000000)

///////////////////////////////////////////////////////////////////////////////
// Definition of Brush Styles
//
// HS_HORIZONTAL: 	00000000	 HS_BDIAGONAL: 	00000001
//		  			00000000		       		00000010
//		  			00000000		       		00000100
//		  			00000000		       		00001000
//		  			11111111		      	 	00010000
//		  			00000000		       		00100000
//		  			00000000		       		01000000
//		  			00000000		       		10000000
//
// HS_VERTICAL:	  	00001000	 HS_CROSS:     	00001000
//		  			00001000		       		00001000
//		  			00001000		       		00001000
//		  			00001000		       		00001000
//		  			00001000		       		11111111
//		  			00001000		       		00001000
//		  			00001000		       		00001000
//		  			00001000		       		00001000
//
// HS_FDIAGONAL: 	10000000	 HS_DIAGCROSS: 	10000001
//		  			01000000		       		01000010
//		  			00100000		       		00100100
//		  			00010000		       		00011000
//		  			00001000		       		00011000
//		  			00000100		       		00100100
//		  			00000010		       		01000010
//		  			00000001		       		10000001
///////////////////////////////////////////////////////////////////////////////
#define HS_HORIZONTAL	0   
#define HS_VERTICAL	   	1
#define HS_FDIAGONAL   	2
#define HS_BDIAGONAL    3
#define HS_CROSS	   	4
#define HS_DIAGCROSS   	5

#define F8x8            0
#define F8x16           1

#define MODE_OPAQUE     		0
#define MODE_TRANSPARENT		1 
#define MODE_SRC_TRANSPARENT    MODE_TRANSPARENT
#define MODE_DEST_TRANSPARENT	2

#define MODE_INSIDE_CLIP    0
#define MODE_OUTSIDE_CLIP   1

#define TYPE_MONO			0
#define TYPE_COLOR			1

#define OPT_PN   			1  // Quadrant 1
#define OPT_NN   			2  // Quadrant 2
#define OPT_NP   			3  // Quadrant 3
#define OPT_PP   			4  // Quadrant 4

extern void 	*_ScreenBufferPtr;
extern void 	*_MonoSrcBufferPtr;
extern void     *_ColorSrcBufferPtr;
extern void 	*_HostBackBufferPtr;
extern BOOL 	_ClipEnable;
extern BOOL     _OutsideClip;
extern UINT32   _ClipTL;
extern UINT32   _ClipBR;
extern char     _DrawMode; 
extern UINT32   _ColorKey;
extern UINT32   _ColorKeyMask;

extern BOOL     _EnableAlpha;
extern int      _AlphaKs;
extern int      _AlphaKd;

extern int      _PatternType;

#ifdef CACHE_ON
#define GFX_OFFSCREEN_ADDR  0x80180000
#define GFX_START_ADDR		0x80200000
#define MONO_SOURCE_ADDR    0x80380000
#define COLOR_SOURCE_ADDR   0x80380000
#define CMODEL_START_ADDR   0x80400000
#else

#ifdef _NUC900_
/*
#define GFX_OFFSCREEN_ADDR  0x180000
#endif
#define GFX_START_ADDR		0x200000
#define MONO_SOURCE_ADDR    0x380000
#define COLOR_SOURCE_ADDR   0x380000
#define CMODEL_START_ADDR   0x400000
#define GFX_PAT_ADDR		(GFX_START_ADDR+GFX_SIZE)
*/

extern __align(32) void *GFX_START_ADDR;
extern __align(32) void *MONO_SOURCE_ADDR;
extern __align(32) void *COLOR_SOURCE_ADDR;
extern __align(32) void *CMODEL_START_ADDR;
extern __align(32) void *GFX_OFFSCREEN_ADDR;
extern __align(32) void *GFX_PAT_ADDR;
#endif
#endif

/*
#define GFX_BPP				16 //16 // can be 8, 16 or 32
#define GFX_WIDTH           240	 //128//128//640 //128 //640
#define GFX_HEIGHT          320  //160//480 //96 //480
#define GFX_PITCH           (GFX_WIDTH*(GFX_BPP/8))
#define GFX_SIZE       		(GFX_HEIGHT*GFX_PITCH)
*/

#define GE_BPP_8                        0x00000000
#define GE_BPP_16                       0x00000010
#define GE_BPP_32                       0x00000020

/* extern from NUC900_GE.c */
extern unsigned int GFX_BPP;     //16 // can be 8, 16 or 32
extern unsigned int GFX_WIDTH;   //128//128//640 //128 //640
extern unsigned int GFX_HEIGHT;   //160//480 //96 //480
extern unsigned int GFX_PITCH;
extern unsigned int GFX_SIZE;


/* Accepted those formats for loading image function  */
#define RGB332 1
#define RGB565 2
#define RGB888 3
//#define RGBMONO 4

/* Dump functions */
extern void dump_screen(char *file_name);
extern void dump_memory(UINT32 start_addr, UINT32 stop_addr, char *file_name);
extern void set_dump_path(char *path);
/************************************************/

/* Loading image from raw file */
//extern int load_image(int format, int width, int height, void *buf, char *filepath);
extern int load_image(int format, int width, int height, void *buf, void *file);
//extern void load_image(int id);
/************************************************/

/* Common functions */
extern unsigned long make_color(int color);
extern void set_write_mask(int mask);
extern void clear_screen(int color);
extern void clear_screen_buffer(void);
extern void set_source_orig_starting(void *ptr);
extern void set_destination_orig_starting(void *ptr);
/************************************************/

/* Initiation or Reset for Graphic Engine */
extern void init_GE(int bpp, int width, int height, void *destination);
extern void reset_GE(void);
extern void reset_GE_FIFO(void);
/************************************************/

/* Set Alpha blending and Clipping */
extern void set_clip(int x1, int y1, int x2, int y2);
extern void set_clip_mode(int opt);
extern void set_alpha_mode(int opt, int ks, int kd);
extern void set_draw_mode(int opt, int ckey, int mask);
/************************************************/

/* 8x8 Pattern Initiation for mono and color */
extern void init_mono_pattern(int opt, int fore_color, int back_color);
extern void init_mono_input_pattern(UINT32 PatternA, UINT32 PatternB, int fore_color, int back_color);
extern void init_color_pattern(int patformat, void *patdata);
/************************************************/

/* Fill the rectangle  on the screen with foreground or background or pattern or tile */
extern void solid_fill(int dx, int dy, int width, int height, int color);
extern void solid_fill_back(int dx, int dy, int width, int height, int color);
extern void mono_pattern_fill(int dx, int dy, int width, int height, int opt);
extern void color_pattern_fill(int dx, int dy, int width, int height);
extern void tile_blt_fill(int srcx, int srcy, int destx, int desty, int width, int height, int x_count, int y_count);
/************************************************/

/* Move display datum from specified place to another place */
extern void screen_to_screen_blt(int sx, int sy, int dx, int dy, int width, int height);
extern void screen_to_screen_rop_blt(int srcx, int srcy, int destx, int desty, int width, int height, int rop);
extern void source_to_destination_blt(int srcx, int srcy, int destx, int desty, int width, int height, int srcpitch, int destpitch);
/************************************************/

/* Move source datum (buf) to display memory */
extern void screen_sprite_blt(int destx, int desty, int sprite_width, int sprite_height, void *buf);
extern void screen_sprite_bltx(int x, int y, int sprite_sx, int sprite_sy, int width, int height, int sprite_width, int sprite_height, void *buf);
extern void screen_sprite_rop_blt(int x, int y, int sprite_width, int sprite_height, void *buf, int rop);
extern void screen_sprite_rop_bltx(int x, int y, int sprite_sx, int sprite_sy, int width, int height, int sprite_width, int sprite_height, void *buf, int rop);
/************************************************/

/* Color expanding BLT */
extern void color_expansion_blt(int x, int y, int width, int height, int fore_color, int back_color, int opt, void *buf);
/************************************************/

/* Rotate specified area on display memory */
extern void rotation(int srcx, int srcy, int destx, int desty, int width, int height, int ctl);
/************************************************/

/* Scale Up/Down */
extern void stretch_bitblt(int srcx, int srcy, int destx, int desty, int width, int height, int hsfN, int hsfM, int vsfN, int vsfM, int updown);
extern void stretch_bitblt_specify(int srcx, int srcy, int destx, int desty, int width, int height, int hsfN, int hsfM, int vsfN, int vsfM, int updown, int srcpitch);
/************************************************/

/* Draw Line  */
extern void draw_solid_line(int x1, int y1, int x2, int y2, int color);
extern void draw_styled_line(int x1, int y1, int x2, int y2, int style, int fgcolor, int bkcolor, int draw_mode);
/************************************************/

/* Draw Frame */
extern void draw_frame(int x1, int y1, int x2, int y2, int color, int opt);
/************************************************/

/* Put the string of 8x8 or 8x16 font on the screen */
extern void set_font_size(int size);
extern void put_string(int x, int y, char *str, int fore_color, int back_color, int draw_mode); 
/************************************************/

/* demo functions */
extern void init_LCM(void);
extern void demo_line_draw(void);
extern void demo_frame_draw(void);
/************************************************/

/* other */
extern void dump_register(void);
extern void dump_cmodel(void);
extern void host_read_blt(int x, int y, int width, int height, void *buf);
extern void host_write_blt(int x, int y, int width, int height, void *buf);
extern void host_sprite_blt(int x, int y, int width, int height, void *buf);
extern void host_color_expansion_blt(int x, int y, int width, int height, int fore_color, int back_color, int opt, void *buf);
extern int  test_reg_GE(void);
extern void memory_copy(void *dest_addr, void *src_addr, int count);
/************************************************/


#endif /*   _NUC900_GE_H_     */